﻿using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;

namespace FlashPay.Util
{
    public static class EncryptHelper
    {
        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string enMD5(string str)
        {
            if (string.IsNullOrEmpty(str) || string.IsNullOrWhiteSpace(str)) return "";
            MD5 md5 = MD5.Create();
            var resultMd5 = md5.ComputeHash(ASCIIEncoding.UTF8.GetBytes(str));
            md5.Clear();
            return BitConverter.ToString(resultMd5).Replace("-", "").ToLower();
        }

        /// <summary>
        /// SHA1加密
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string enSHA1(string str)
        {
            if (string.IsNullOrEmpty(str) || string.IsNullOrWhiteSpace(str)) return "";
            SHA1 sha1 = new SHA1CryptoServiceProvider();
            var resultSha1 = sha1.ComputeHash(ASCIIEncoding.UTF8.GetBytes(str));
            sha1.Clear();
            return BitConverter.ToString(resultSha1).Replace("-", "").ToLower();
        }

        /// <summary>
        /// SHA256加密
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string enSHA256(string str)
        {
            if (string.IsNullOrEmpty(str) || string.IsNullOrWhiteSpace(str)) return "";
            SHA256 sha256 = new SHA256CryptoServiceProvider();
            var resultSha256 = sha256.ComputeHash(ASCIIEncoding.UTF8.GetBytes(str));
            sha256.Clear();
            return BitConverter.ToString(resultSha256).Replace("-", "").ToLower();
        }

        /// <summary>
        /// SHA384加密
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string enSHA384(string str)
        {
            if (string.IsNullOrEmpty(str) || string.IsNullOrWhiteSpace(str)) return "";
            SHA384 sha384 = new SHA384CryptoServiceProvider();
            var resultSha384 = sha384.ComputeHash(ASCIIEncoding.UTF8.GetBytes(str));
            sha384.Clear();
            return BitConverter.ToString(resultSha384).Replace("-", "").ToLower();
        }

        /// <summary>
        /// SHA512加密
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string enSHA512(string str)
        {
            if (string.IsNullOrEmpty(str) || string.IsNullOrWhiteSpace(str)) return "";
            SHA512 sha512 = new SHA512CryptoServiceProvider();
            var resultSha512 = sha512.ComputeHash(ASCIIEncoding.UTF8.GetBytes(str));
            sha512.Clear();
            return BitConverter.ToString(resultSha512).Replace("-", "").ToLower();
        }

        private const string DefaultEncryptKey = "04ef51bcd5ebe089d3fa84afa7810af704ef51bcd5ebe0";
        private const string CS_Config_EncryptKey = "EncryptKey";
        //默认密钥向量
        private static readonly byte[] Keys = { 0xBF, 0xAC, 0x26, 0x78, 0x90, 0x34, 0x1D, 0x1F };
        /// <summary>
        /// DES加密字符串
        /// </summary>
        /// <param name="encryptString">待加密的字符串</param>
        /// <param name="encryptKey">加密密钥,要求为8位</param>
        /// <returns>加密成功返回加密后的字符串，失败返回源串</returns>
        public static string enCryptDES(string encryptString)
        {
            string strEncryptKey = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build()[CS_Config_EncryptKey];
            if (string.IsNullOrEmpty(strEncryptKey))
            {
                strEncryptKey = DefaultEncryptKey;
            }
            try
            {
                byte[] rgbKey = Encoding.UTF8.GetBytes(strEncryptKey.Substring(0, 8));
                byte[] rgbIV = Keys;
                byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
                DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
                System.IO.MemoryStream mStream = new System.IO.MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                return Convert.ToBase64String(mStream.ToArray());
            }
            catch
            {
                return encryptString;
            }
        }

        /// <summary>
        /// DES解密字符串
        /// </summary>
        /// <param name="decryptString">待解密的字符串</param>
        /// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param>
        /// <returns>解密成功返回解密后的字符串，失败返源串</returns>
        public static string deCryptDES(string decryptString)
        {
            string strEncryptKey = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build()[CS_Config_EncryptKey];
            if (string.IsNullOrEmpty(strEncryptKey))
            {
                strEncryptKey = DefaultEncryptKey;
            }
            try
            {
                byte[] rgbKey = Encoding.UTF8.GetBytes(strEncryptKey.Substring(0, 8));
                byte[] rgbIV = Keys;
                byte[] inputByteArray = Convert.FromBase64String(decryptString);
                DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
                System.IO.MemoryStream mStream = new System.IO.MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                return Encoding.UTF8.GetString(mStream.ToArray());
            }
            catch
            {
                return decryptString;
            }
        }
    }
}
